﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace ConsoleApplication1
{
    class DataAccess
    {
        public void AppPatient(Patient p)
        {

            
            string idd="";
            string[] str1 = File.ReadAllLines(".\\..\\..\\in.txt");

            if (str1.Length == 0)
            {
                p.id = 1;
            }
            else
            {
                idd = str1[str1.Length - 1].Substring(0, str1[str1.Length - 1].IndexOf(","));
                p.id = Convert.ToInt32(idd);
                p.id++;
            }
            //System.Console.WriteLine(str1.Length);
            
            //System.Console.WriteLine(str1.Length);
            string str = p.id.ToString() + ",";
            str += p.name + ",";
            str += p.surname + ",";
            str += p.patronymic + ",";
            str += p.dateOfBirth.ToString("dd/MM/yyyy ");

            //idd = str.Substring(str.IndexOf("\n"), str.IndexOf(","));
            //System.Console.WriteLine(idd);

            

            //System.Console.WriteLine(idd);

            File.AppendAllText(".\\..\\..\\in.txt", str + "\n");

            //idd = str.Substring(str.IndexOf("\n"), str.IndexOf(","));
            //System.Console.WriteLine(idd);

            //p.id = Convert.ToInt32(idd);
            //p.id++;


            //или
            //string.Format("{0},{1},{2},{3},{4}",p.id,p.name,p.surname,p.patronymic,p.dateOfBirth);
            
            //idd = str.Substring(0, str.IndexOf(","));
            //System.Console.WriteLine(str);

            //p.id = Convert.ToInt32(idd);
            //p.id++;
            
            // p.id.CompareTo(idd1++);
            //System.Console.WriteLine(idd1);
            //File.AppendAllText("K:\\Новая папка\\ConsoleApplication2(1)\\ConsoleApplication2\\ConsoleApplication1\\in.txt", string.Format("{0},{1},{2},{3},{4}", p.id, p.name, p.surname, p.patronymic, p.dateOfBirth.ToString("dd/MM/yyyy")) + "\n");
            
           // str.IndexOf(",");
            
 
             
            //string str = "10,sss,aaa,ddd,10-nov-2014";
            //Patient p1 = new Patient();
            //string[] elemants = str.Split(',');
            //p1.id = Convert.ToInt32(elemants[0]);
            //p1.name = elemants[1];
            //p1.dateOfBirth = Convert.ToDateTime(elemants[4]);

        }

        public void UpdatePatient(Patient p)
        {
            ArrayList list = new ArrayList();
            string[] str1 = File.ReadAllLines(".\\..\\..\\in.txt");
            string[] k={};
            File.WriteAllText(".\\..\\..\\in.txt", "");
    
            for (int i = 0; i < str1.Length; i++)
            {
                Patient patient1 = new Patient();
                k = str1[i].Split(',');
                if (Convert.ToInt32(k[0]) == p.id)
                {
                    if (p.name == null)
                    {
                        p.name = k[1];
                    }
                    if (p.surname == null)
                    {
                        p.surname = k[2];
                    }
                    if (p.patronymic == null)
                    {
                        p.patronymic = k[3];
                    }
                    if (DateTime.MinValue == null)
                    {
                        p.dateOfBirth = Convert.ToDateTime(k[4]);
                    }
                    //str1
                    //string.Join(',', p.id,p.name,p.surname,p.patronymic,p.dateOfBirth.ToString("dd/MM/yyyy "););
                    str1[i] = p.id + "," + p.name + "," + p.surname + "," + p.patronymic + "," + p.dateOfBirth.ToString("dd/MM/yyyy ");
                    //System.Console.WriteLine(str1[i]);
                }
                
                File.AppendAllText(".\\..\\..\\in.txt", str1[i] + "\n");

            }


        }

        public void deletePatient(Patient p)
        {
            ArrayList list = new ArrayList();

            string[] str1 = File.ReadAllLines(".\\..\\..\\in.txt");

            string[] k;

            for (int i = 0; i < str1.Length; i++)
            {
                k = str1[i].Split(',');
                if (Convert.ToInt32(k[0]) != p.id)
                {
                    list.Add(str1[i]);
                }
            }

            File.WriteAllText(".\\..\\..\\in.txt", "");
            for (int i = 0; i < list.Count; i++)
            {
                File.AppendAllText(".\\..\\..\\in.txt", list[i] + "\n");
            }
            
        }



        public ArrayList list()
        {
            ArrayList list = new ArrayList();

            string[] str1 = File.ReadAllLines(".\\..\\..\\in.txt");
            
            //System.Console.WriteLine(str1[1].Split(','));
            //System.Console.WriteLine("111");
            //Patient patient1 = new Patient();

            string[] k;

            for (int i=0; i<str1.Length;i++)
            {
               Patient patient1 = new Patient();
               k = str1[i].Split(',');
               patient1.id = Convert.ToInt32(k[0]);
               patient1.name = k[1];
               patient1.surname = k[2];
               patient1.patronymic = k[3];
               patient1.dateOfBirth = Convert.ToDateTime(k[4]);
               list.Add(patient1);
            }
            

            //System.Console.WriteLine(k[1]);
            //string[] k;

            //list.Add(p.name + p.surname + p.patronymic + p.dateOfBirth);



            /*
            int kol=0,kk=0;
            for (int i = 0; i < str1.Length; i++)
            {
                if (str1[i] == " ")
                {
                    kol++;
                }
                kk = str1[i].Split(' ');
            }
             * */
            /*
            int[,] array1 = new int[str1.Length, str1.Length];

            string[] k;
            string s = "";
            for (int i = 0; i < str1.Length; i++)
            {
                k = str1[i].Split(' ');
                for (int j = 0; j < str1.Length; j++)
                {
                    array1[i, j] = (k[j]);
                    s += array1[i, j] + ",";
                    if (j > str1.Length - 2)
                    {
                        s += "\n";
                    }
                }
            }
            Console.WriteLine(s);
            Console.ReadLine();

           //File.ReadAllLines("C:\\Documents and Settings\\Student\\My Documents\\Alex2\\ConsoleApplication2(1)\\ConsoleApplication2\\ConsoleApplication1\\in.txt");

            list.Add(File.ReadAllLines("K:\\Новая папка\\ConsoleApplication2(1)\\ConsoleApplication2\\ConsoleApplication1\\in.txt"));
            foreach (object o in list)
            {
                Console.WriteLine(o);
            }

            */
   
            return list;
            
        }
    }
}
